﻿// -----------------------------------------------------------------------
// <copyright file="UsersImpl.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

using System.Collections.Generic;
using System.Data.Common;
using StampController.BL.Models;
using StampController.Core;

namespace StampController.BL
{
    /// <summary>
    ///     TODO: Update summary.
    /// </summary>
    public class UsersImpl
    {
        public List<UsersModel> GetUserList()
        {
            var list = new List<UsersModel>();
            using (var dbHelper = new DbHelper())
            {
                using (
                    DbDataReader reader =
                        dbHelper.ExecuteReader(
                            "SELECT A.UserID, A.Name, Email, IsHOD, IsGD, A.DeptID, B.Name AS DeptName, SiteID FROM Users A LEFT JOIN Department B ON A.DeptID = B.DeptID"))
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            list.Add(new UsersModel
                            {
                                UserID = reader.GetString(0),
                                Name = reader.GetString(1),
                                Email = reader.Get<string>(2),
                                IsHOD = reader.Get<bool>(3),
                                IsGD = reader.Get<bool>(4),
                                DeptID = reader.Get<int?>(5),
                                DeptName = reader.Get<string>(6),
                                SiteID = reader.Get<int>(7)
                            });
                        }
                    }

                    reader.Close();
                }

                return list;
            }
        }

        public UsersModel GetUser(string userId)
        {
            UsersModel user = null;
            using (var dbHelper = new DbHelper())
            {
                object[,] parameter =
                {
                    {"@UserID", userId}
                };
                using (
                    DbDataReader reader =
                        dbHelper.ExecuteReader(
                            "SELECT TOP 1 A.Name, Email, IsHOD, IsGD, A.DeptID, B.Name AS DeptName, SiteID, IsAdminOfficer, IsAdminSite FROM Users A LEFT JOIN Department B ON A.DeptID = B.DeptID WHERE UserID = @UserID",
                            parameter))
                {
                    if (reader.HasRows && reader.Read())
                    {
                        user = new UsersModel
                        {
                            UserID = userId,
                            Name = reader.GetString(0),
                            Email = reader.Get<string>(1),
                            IsHOD = reader.Get<bool>(2),
                            IsGD = reader.Get<bool>(3),
                            DeptID = reader.Get<int?>(4),
                            DeptName = reader.Get<string>(5),
                            SiteID = reader.Get<int>(6),
                            IsAdminOfficer = reader.Get<bool>(7),
                            IsAdminSite = reader.Get<bool>(8)
                        };
                    }

                    reader.Close();
                }

                return user;
            }
        }

        public UsersModel GetHod(int? deptId)
        {
            if (deptId == Common.GdDept) return GetGd();
            UsersModel user = null;
            using (var dbHelper = new DbHelper())
            {
                object[,] parameter =
                {
                    {"@DeptID", deptId}
                };
                using (
                    DbDataReader reader =
                        dbHelper.ExecuteReader(
                            "SELECT TOP 1 A.UserID, A.Name, Email, IsHOD, IsGD, A.DeptID, B.Name AS DeptName, SiteID FROM Users A LEFT JOIN Department B ON A.DeptID = B.DeptID WHERE A.DeptID = @DeptID AND IsHOD = 1",
                            parameter))
                {
                    if (reader.HasRows && reader.Read())
                    {
                        user = new UsersModel
                        {
                            UserID = reader.GetString(0),
                            Name = reader.GetString(1),
                            Email = reader.Get<string>(2),
                            IsHOD = reader.Get<bool>(3),
                            IsGD = reader.Get<bool>(4),
                            DeptID = reader.Get<int?>(5),
                            DeptName = reader.Get<string>(6),
                            SiteID = reader.Get<int>(7)
                        };
                    }

                    reader.Close();
                }
            }

            return user;
        }

        public UsersModel GetGd()
        {
            UsersModel user = null;
            using (var dbHelper = new DbHelper())
            {
                using (
                    DbDataReader reader =
                        dbHelper.ExecuteReader(
                            "SELECT TOP 1 A.UserID, A.Name, Email, IsHOD, IsGD, A.DeptID, B.Name AS DeptName, SiteID FROM Users A LEFT JOIN Department B ON A.DeptID = B.DeptID WHERE IsGD = 1"))
                {
                    if (reader.HasRows && reader.Read())
                    {
                        user = new UsersModel
                        {
                            UserID=reader.Get<string>(0),
                            Name = reader.GetString(1),
                            Email = reader.Get<string>(2),
                            IsHOD = reader.Get<bool>(3),
                            IsGD = reader.Get<bool>(4),
                            DeptID = reader.Get<int?>(5),
                            DeptName = reader.Get<string>(6),
                            SiteID = reader.Get<int>(7)
                        };
                    }

                    reader.Close();
                }
            }

            return user;
        }

        public UsersModel GetAdminOfficer(int siteId)
        {
            UsersModel user = null;
            using (var dbHelper = new DbHelper())
            {
                // SiteID = 1 (CTY - HN), SiteID = 2 (CN - HCM)
                object[,] parameter =
                {
                    {"@SiteID", siteId}
                };
                using (
                    DbDataReader reader =
                        dbHelper.ExecuteReader(
                            "SELECT TOP 1 A.Name, Email, IsHOD, IsGD, A.DeptID, " +
                            "B.Name AS DeptName, SiteID " +
                            "FROM Users A " +
                            "LEFT JOIN Department B ON A.DeptID = B.DeptID " +
                            "WHERE IsAdminOfficer = 1 AND SiteID = @SiteID", parameter))
                {
                    if (reader.HasRows && reader.Read())
                    {
                        user = new UsersModel
                        {
                            Name = reader.GetString(0),
                            Email = reader.Get<string>(1),
                            IsHOD = reader.Get<bool>(2),
                            IsGD = reader.Get<bool>(3),
                            DeptID = reader.Get<int?>(4),
                            DeptName = reader.Get<string>(5),
                            SiteID = reader.Get<int>(6)
                        };
                    }

                    reader.Close();
                }
            }

            return user;
        }
    }
}